use strict;
use warnings;

package Lingua::EN::WSD::WebBased::Expander::Google;

use base qw/Lingua::EN::WSD::WebBased::Expander/;

use REST::Google::Search;
use POSIX qw/ceil/;

use Lingua::EN::WSD::WebBased::Utils;
use Lingua::EN::WSD::WebBased::Logger;

use Class::AccessorMaker {
    http_referer        => 'http://www.cpan.org'
}, 'new_init';

sub init {

    my $self = shift;

    $self->SUPER::init;
    
    REST::Google::Search->http_referer($self->http_referer);

}

sub search {  

    my ($self, $query, $size) = @_;
    
    _trace(__PACKAGE__, "::search($query, $size)");
    
    my @documents;

    $size ||= 32;
    $size = 32 if $size > 32;
    my $page_size = 8;
    my $pages = ceil($size / $page_size);

    for my $n (0 .. $pages - 1) {
        
        my $start = $n * $page_size;
        
        my ($response, $status);
        eval {
            $response = REST::Google::Search->new(
                q       => $query,
                rsz     => 'large',
                start   => $start,
                hl      => 'en'
            );
            $status = $response->responseStatus;
        };

        if (!defined $response || !defined $status || $status != 200) {
            return undef;
        }
        else {
            my @results = $response->responseData->results;
            for my $i (0 .. ($size - $start - 1)) {
                last if $i > $#results;
                push @documents, $results[$i]->content;
            }
        }

    }

    _warn("expected documents set: $size, got: ", scalar @documents)
        if $size != @documents;

    return \@documents;

}

1;
